//
//  LZKImageAddition.m
//  lzk
//
//  Created by mengchangMBP on 8/18/16.
//  Copyright © 2016 zjn. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <Photos/Photos.h>

#import "LZKImageAddition.h"

@implementation UIImage (LZKImageAddition)

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // Create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

+ (UIImage *)imageResize:(UIImage *)image WithMaxSize:(CGFloat)maxSize {
    
    if(!image) {
        return nil;
    }
    
    if (image.size.height <= maxSize && image.size.width <= maxSize) {
        return image;
    }
    
    CGFloat imageDrawWidth = image.size.width;
    CGFloat imageDrawHeight = image.size.height;
    
    if(imageDrawWidth > imageDrawHeight) {
        // 宽度大
        CGFloat scale = maxSize / imageDrawWidth;
        imageDrawWidth = maxSize;
        imageDrawHeight *= scale;
    } else {
        // 高度大
        CGFloat scale = maxSize / imageDrawHeight;
        imageDrawHeight = maxSize;
        imageDrawWidth *= scale;
    }
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageDrawWidth, imageDrawHeight),
                                           NO,
                                           1);
    
//    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageDrawWidth, imageDrawHeight),
//                                           NO,
//                                           [UIScreen mainScreen].scale);
    
    [image drawInRect:CGRectMake(0, 0, imageDrawWidth, imageDrawHeight)];
    
    UIImage *imageResized = UIGraphicsGetImageFromCurrentImageContext();
        
    UIGraphicsEndImageContext();
    
    return imageResized;
}

//保存图片
+ (void)saveImage2PHPhotoLibrary:(UIImage *)image {

    if(!image) {
        return;
    }
    
    if([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined) {//用户还没有作出选择
        
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                [UIImage saveImage2PHPhotoLibrary:image];
            }else{
            }
        }];
        
        return;
    }
    
    if(PHAuthorizationStatusAuthorized != [PHPhotoLibrary authorizationStatus]) {
        return;
    }
    
    __block  NSString *assetLocalIdentifier;
    
    //__weak typeof(self) weakSelf = self;

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        //1.保存图片到相机胶卷中----创建图片的请求
        assetLocalIdentifier = [PHAssetCreationRequest creationRequestForAssetFromImage:image].placeholderForCreatedAsset.localIdentifier;
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        
        //if(! weakSelf) { return; }
        //__strong typeof(weakSelf) strongSelf = weakSelf;
        
    }];

}

@end
